
package com.moogene.saad.gameplay.attacter;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;

import com.moogene.android.engine.animlite.Animlite;
import com.moogene.android.engine.mmi.Book;
import com.moogene.android.engine.mmi.Layer;

public class Attacker extends Layer {
    private int x = -20;

    private int y = 230;

    private final AttAction action;
    
    private final int HP_RECT_WIDTH = 60;
    
    private final int HP_GAP_Y = 25;
    
    private final int HP_RECT_HEIGHT = 10;

    private int speed;
    
    private int hpFull;

	private int hp;

//    private boolean isUpdate = false;
    
    private Paint paint = new Paint();
    
    private Rect hpRect = new Rect();

    protected Attacker(AttAction action, int speed, int hp) {
        super(action.getAnimlite().getFrameWidth(), action.getAnimlite().getFrameHeight());
        this.action = action;
        this.speed = speed;
        this.hpFull = hp;
        this.hp = hp;
    }

    public static Attacker load(String name){
    	Attacker attacker = null;
    	if(name.equalsIgnoreCase("enemy1")){
    		String actionName = "actEnemy1";
    		AttAction action = AttAction.load(actionName);
    		int speed = 3;
    		int hp = 100;
    		attacker = new Attacker(action, speed, hp);
    	}
    	return attacker;
    }

    @Override
    protected Animlite getAttachedAnimlite() {
        return action.getAnimlite();
    }
    
    @Override
    public void draw(Canvas canvas) {
    	super.draw(canvas);
    	hpRect.left = x - (HP_RECT_WIDTH >> 1);
    	hpRect.right = x + (HP_RECT_WIDTH >> 1);
    	hpRect.top = y - action.getAnimlite().getAnchor().getAnchorY() - HP_GAP_Y;
    	hpRect.bottom = hpRect.top + HP_RECT_HEIGHT;
    	paint.setColor(0xFF60FCFF);
    	paint.setStyle(Style.STROKE);
    	canvas.drawRect(hpRect, paint);
    	hpRect.inset(1, 1);
    	hpRect.right += 1;
    	paint.setColor(Color.RED);
    	paint.setStyle(Style.FILL);
    	int width = hpRect.width() * hp / hpFull;
    	hpRect.right = hpRect.left + width;
    	canvas.drawRect(hpRect, paint);
    }

    public void pass() {
        // TODO Auto-generated method stub

    }

    private void die() {
        action.die();
        Book.getInstance().removeLayer(this);
    }

    @Override
    public void update() {
        x += speed;
        setLocation(x, y);
        action.move();
    }

//    public void enter() {
//        isUpdate = true;
//    }

    public void isHurt(int hurtHp){
    	decreaseHp(hurtHp);
    	action.hurt();
    	if(hp <= 0){
    		die();
    	}
    }
    
    public boolean isDead(){
    	return hp == 0;
    }

    private int decreaseHp(int dhp) {
		this.hp -= dhp;
		if(this.hp <= 0){
			this.hp = 0;
		}
		return this.hp;
	}

//    @Override
//	public Rect getDstRect() {
//		if (action != null) {
//			return action.getAnimlite().getDstRect(x, y);
//		}
//		return null;
//	}
}
